<?php
/**
 * Created by PhpStorm.
 * User: jsd
 * Date: 2016/8/31
 * Time: 14:33
 */
namespace Admin\Api;

use \Common\Api\Api;
use Shop\Model\ProductModel;

class ZengwuxianApi extends Api
{
    const QUERY_LIST = "Admin/Zengwuxian/queryList";
    const DETAIL = "Admin/Zengwuxian/detail";

    //初始化模型类
    protected function _init()
    {
    }

    //赠产品查询
    public function queryList($map = null, $page = array('curpage' => 0, 'size' => 10), $order = false, $params = false, $fields = false)
    {
        $yuan = new ProductModel();
        $list = $yuan->alias('p')->field('p.id as pid,name as product_name,createtime,detail,synopsis,dt_goods_unit,u.id as uid,username,nickname,head')
            ->join('__PRODUCT_ATTR__ as pa ON p.id = pa.pid', 'left')
            ->join('v_user_normal as u ON p.uid = u.id', 'left')
            ->where('pa.is_second=1')->page($page)->select();
        if ($list === false) {
            $error = $this->model->getDbError();
            return $this->apiReturnErr($error);
        }
        $count = $yuan->alias('p')->field('p.id as pid,name as product_name,createtime,detail,synopsis,dt_goods_unit,u.id as uid,username,nickname,head')
            ->join('__PRODUCT_ATTR__ as pa ON p.id = pa.pid', 'left')
            ->join('v_user_normal as u ON p.uid = u.id', 'left')
            ->where('pa.is_second=1')->count();
        $Page = new \Think\Page($count);// 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show();
        return $this->apiReturnSuc(array("show" => $show, "list" => $list));

    }

    //赠产品详情
    public function detail($map)
    {
        $sourse = new ProductModel();
        $result = $sourse->alias('p')->field('p.id as pid,name as product_name,createtime,detail,synopsis,dt_goods_unit,u.id as uid,username,nickname,head')
            ->join('__PRODUCT_ATTR__ as pa ON p.id = pa.pid', 'left')
            ->join('v_user_normal as u ON p.uid = u.id', 'left')
            ->where("pid=$map")->find();
        if ($result) {
            return $this->apiReturnErr($result);
        }
        return $this->apiReturnSuc($result);
    }
}

